/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_QUATERNION_HPP
#define XERN_QUATERNION_HPP

#include <boost/io/ios_state.hpp>
#include <iostream>
#include "MathCommon.hpp"
#include "Vector.hpp"

#ifdef XERN_USE_BULLET
// Bullet include
#include <LinearMath/btQuaternion.h>
#endif

namespace Xern
{

class XERN_ALIGNED(16) Quaternion: public QuadFloat
{
public:

    Quaternion()
    {
        w = 1.0f;
        x = 0.0f;
        y = 0.0f;
        z = 0.0f;
    }

    Quaternion(float s)
    {
        w = s;
        x = s;
        y = s;
        z = s;
    }

    Quaternion(float cw, float cx, float cy, float cz)
    {
        w = cw;
        x = cx;
        y = cy;
        z = cz;
    }

    Quaternion(float ex, float ey, float ez)
    {
        SetEuler(ex, ey, ez);
    }

    Quaternion(float cw, const Vector3 &v)
    {
    	w = cw;
    	x = v.x;
    	y = v.y;
    	z = v.z;
    }

    Quaternion(const Quaternion &q)
    {
        w = q.w;
        x = q.x;
        y = q.y;
        z = q.z;
    }

#ifdef XERN_USE_BULLET
    Quaternion(const btQuaternion &q)
    {
        w = q.w();
        x = q.x();
        y = q.y();
        z = q.z();
    }
#endif

    float Length() const
    {
        return squareroot(x*x + y*y + z*z + w*w);
    }

    float Length2() const
    {
        return x*x + y*y + z*z + w*w;
    }

    void Normalize()
    {
        float l = invsquareroot(x*x + y*y + z*z + w*w);
        w *= l;
        x *= l;
        y *= l;
        z *= l;
    }

    Quaternion Conjugate() const
    {
        return Quaternion(w, -x, -y, -z);
    }

    Quaternion Inverse() const
    {
        Quaternion qp = Conjugate();
        return qp/Length2();
    }

    void Set(float cw, float cx, float cy, float cz)
    {
        w = cw;
        x = cx;
        y = cy;
        z = cz;
    }

    void Set(const Quaternion &q)
    {
        w = q.w;
        x = q.x;
        y = q.y;
        z = q.z;
    }

#ifdef XERN_USE_BULLET
    void Set(const btQuaternion &q)
    {
        w = q.getW();
        x = q.getX();
        y = q.getY();
        z = q.getZ();
    }

    operator btQuaternion()
    {
        return btQuaternion(x, y, z, w);
    }

#endif

    void SetEuler(float ex, float ey, float ez)
    {
        Quaternion qx(cosine(ex/2), sine(ex/2), 0, 0);
        Quaternion qy(cosine(ey/2), 0, sine(ey/2), 0);
        Quaternion qz(cosine(ez/2), 0, 0, sine(ez/2));
        Set(qx*qy*qz);
    }

    const Quaternion &operator=(const Quaternion &q)
    {
        w = q.w;
        x = q.x;
        y = q.y;
        z = q.z;
        return *this;
    }

    const Quaternion &operator+=(const Quaternion &q)
    {
        w += q.w;
        x += q.x;
        y += q.y;
        z += q.z;
        return *this;
    }

    const Quaternion &operator-=(const Quaternion &q)
    {
        w -= q.w;
        x -= q.x;
        y -= q.y;
        z -= q.z;
        return *this;
    }

    const Quaternion &operator*=(float s)
	{
    	w *= s;
    	x *= s;
    	y *= s;
    	z *= s;
    	return *this;
	}

    const Quaternion &operator*=(const Quaternion &q)
	{
    	*this = (*this)*q;
    	return *this;
	}

    const Quaternion &operator/=(float s)
    {
       	s = 1.0f/s;
       	w *= s;
       	x *= s;
       	y *= s;
       	z *= s;
       	return *this;
    }

    friend bool operator==(const Quaternion &q1, const Quaternion &q2)
    {
        return q1.x==q2.x && q1.y==q2.y && q1.z==q2.z && q1.w==q2.w;
    }

    friend Quaternion operator+(const Quaternion &q1, const Quaternion &q2)
    {
        return Quaternion(q1.r + q2.r, q1.i + q2.i, q1.j + q2.j, q1.k + q2.k);
    }

    friend Quaternion operator-(const Quaternion &q1, const Quaternion &q2)
    {
        return Quaternion(q1.r - q2.r, q1.i - q2.i, q1.j - q2.j, q1.k - q2.k);
    }

    friend Quaternion operator*(const Quaternion &q1, const Quaternion &q2)
    {
        return Quaternion(q1.r*q2.r - q1.i*q2.i - q1.j*q2.j - q1.k*q2.k,
                          q1.r*q2.i + q1.i*q2.r + q1.j*q2.k - q1.k*q2.j,
                          q1.r*q2.j - q1.i*q2.k + q1.j*q2.r + q1.k*q2.i,
                          q1.r*q2.k + q1.i*q2.j - q1.j*q2.i + q1.k*q2.r);
    }

    friend Quaternion operator*(const Quaternion &q, const float s)
    {
        return Quaternion(q.r*s, q.i*s, q.j*s, q.k*s);
    }

    friend Quaternion operator*(const float s, const Quaternion &q)
    {
        return Quaternion(q.r*s, q.i*s, q.j*s, q.k*s);
    }

    friend Vector3 operator*(const Quaternion &q, const Vector3 &v)
    {
        Quaternion temp =  q * Quaternion(0.0, v);
        return Vector3(temp.x, temp.y, temp.z);
    }

    friend Vector3 operator*(const Vector3 &v, const Quaternion &q)
    {
        Quaternion temp =  Quaternion(0.0, v) * q;
        return Vector3(temp.x, temp.y, temp.z);
    }

    friend Quaternion operator/(const Quaternion &q, const float s)
    {
        return q*(1.0f/s);
    }
};

inline std::ostream &operator<<(std::ostream &os, const Quaternion &q)
{
    os << q.x << " " << q.y << " " << q.z << " " << q.w;
    return os;
}

inline std::istream &operator>>(std::istream &is, const Quaternion &q)
{
	boost::io::ios_flags_saver s(is);
    is >> std::skipws >> q.x >> q.y >> q.z >> q.w;
    return is;
}

};

#endif //XERN_QUATERNION_HPP

